package com.example.xpvideo.tools;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;


public class Iputil {

        public static String getLocalIP (String defaultIp){
            String ip = defaultIp;
            try {
                ip = InetAddress.getLocalHost()
                        .getHostAddress();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            return ip;
        }

        /**
         * 获取真实ip地址 通过阿帕奇代理的也能获取到真实ip
         * @param request
         * @return
         */
        public static String getRealIp (HttpServletRequest request){
            String ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            return ip;
        }
    }



/* Location:              C:\Users\Alan\Desktop\WEB-INF\classes.zip!\classes\com\demo\miniShop\tools\Iputil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */